/**
 * Free software.
 */

package org.nanhill.moo.domain.search.favor;

import org.hibernate.Session;
import org.nanhill.commons.pattern.search.SearchException;
import org.nanhill.support.hibernate.search.HqlSearchVisitor;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-11-13
 */
public class HibernateFavorCountVisitor extends HqlSearchVisitor implements
		FavorVisitor {

	private long count;

	public long getCount() {
		return count;
	}

	public HibernateFavorCountVisitor() {
		super();
	}

	public HibernateFavorCountVisitor(Session session) {
		super(session);
	}

	public void visitAuthorId(FavorCriterion criterion)
			throws SearchException {
		addCriterion("author.id=?", criterion.getValue());

	}

	public void visitCategoryId(FavorCriterion criterion)
			throws SearchException {
		addCriterion("category.id=?", criterion.getValue());
	}

	public void visitCostId(FavorCriterion criterion)
			throws SearchException {
		addCriterion("cost.id=?", criterion.getValue());
	}

	public void visitIdOrder(FavorCriterion criterion) throws SearchException {
		throw new UnsupportedOperationException();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.support.hibernate.search.HqlSearchVisitor#buildFrom()
	 */
	@Override
	protected void buildFrom() {
		append("From Favor ");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.support.hibernate.search.HqlSearchVisitor#buildLimit()
	 */
	@Override
	protected void buildLimit() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.support.hibernate.search.HqlSearchVisitor#buildSelect()
	 */
	@Override
	protected void buildSelect() {
		append("SELECT COUNT(id) ");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.commons.pattern.search.AbstractSearchVisitor#doInternalSearch()
	 */
	@Override
	protected void doInternalSearch() throws SearchException {
		count = (Long) getQuery().uniqueResult();
	}

}
